package Model.Dao;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import Model.Pojo.Khachhang;
import Model.Pojo.Parameters;
import Model.Util.HibernateUtil;
import java.util.List;
import java.util.logging.Logger;
import org.hibernate.Session;

/**
 *
 * @author Administrator
 */
public class DAOKhachHang
{

    /**
     * Them khach hang
     *
     * @param dtoKH
     * @throws Exception
     */
    public void InsertKhacHang(Khachhang dtoKH) throws Exception
    {
        Session session = HibernateUtil.getSessionFactory().openSession();

        //Kiem tra khach hang co trong database hay khong?

        //Insert vao database neu chua co khach hang
        DAOProvider.Insert(session, dtoKH);

        session.close();
    }

    /**
     * update thong tin khach hang
     *
     * @param dtoKH
     * @throws Exception
     */
    public void UpdateKhachHang(Khachhang dtoKH) throws Exception
    {
        Session session = HibernateUtil.getSessionFactory().openSession();

        //Kiem tra khach hang co trong database hay khong?

        //Insert vao database neu chua co khach hang
        DAOProvider.Update(session, dtoKH);

        session.close();
    }

    /**
     * Lay danh sach khach hang
     *
     * @param tinhtrang
     * @return
     */
    public List LayDanhSachKhachHang(boolean tinhtrang) throws Exception
    {
        Session session = Model.Util.HibernateUtil.getSessionFactory().openSession();

        String hql = "from Khachhang kh where kh.tinhtrang =:tinhtrang";

        try
        {
            return DAOProvider.Select(session, hql, new Parameters[]
            {
                new Parameters("tinhtrang", tinhtrang)
            });
        }
        catch (Exception ex)
        {
            System.out.print(ex.getMessage());
            throw new Exception(ex.getMessage());
        }
        finally
        {
            session.close();
        }
    }

    /**
     * Chi tiet khach hang
     *
     * @param id_khachhang
     * @return
     */
    public Khachhang ChiTieKhachHang(int id_khachhang) throws Exception
    {
        Session session = Model.Util.HibernateUtil.getSessionFactory().openSession();

        String hql = "from Khachhang kh where kh.id =:id_khachhang";

        try
        {
            List query = DAOProvider.Select(session, hql, new Parameters[]
            {
                new Parameters("id_khachhang", id_khachhang)
            });
            return query != null ? (Khachhang) query.get(0) : null;
        }
        catch (Exception ex)
        {
            System.out.print(ex.getMessage());
            throw new Exception(ex.getMessage());
        }
        finally
        {
            session.close();
        }
    }

    /**
     * Xoa khach hang
     */
    public void XoaKhachHang(int id_khachhang) throws Exception
    {
        Session session = Model.Util.HibernateUtil.getSessionFactory().openSession();

        try
        {
            DAOProvider.Delete(session, DAOProvider.
                    SelectOne(session, Khachhang.class, id_khachhang));
        }
        catch (Exception ex)
        {
            System.out.print(ex.getMessage());
            throw new Exception(ex.getMessage());
        }
        finally
        {
            session.close();
        }
    }

    public List TimKiemKhachHang(String khachhang, String hoten, String diachi, String dienthoai,
                                 String email) throws Exception
    {
        Session session = Model.Util.HibernateUtil.getSessionFactory().openSession();


        if (khachhang.trim() != "")
        {
            String hql = "from Khachhang kh where kh.tinhtrang = true and kh.makh =:khachhang";
            try
            {
                return DAOProvider.Select(session, hql, new Parameters[]
                {
                    new Parameters("khachhang", khachhang.trim())
                });
            }
            catch (Exception ex)
            {
                System.out.print(ex.getMessage());
                throw new Exception(ex.getMessage());
            }
            finally
            {
                session.close();
            }
        }
        else
        {
            String hql = "from Khachhang kh where kh.tinhtrang = true and kh.makh =:khachhang or ";
            hql += "(kh.hoten like :hoten and kh.diachi like :diachi and kh.sodienthoai like :dienthoai and kh.email like :email)";
            try
            {
                return DAOProvider.Select(session, hql, new Parameters[]
                {
                    new Parameters("khachhang", khachhang.trim()), new Parameters("hoten",
                                                                                  "%" + hoten.
                    trim() + "%"),
                    new Parameters("diachi", "%" + diachi.trim() + "%"), new Parameters("dienthoai",
                                                                                        "%" + dienthoai.
                    trim() + "%"),
                    new Parameters("email", "%" + email.trim() + "%")
                });
            }
            catch (Exception ex)
            {
                System.out.print(ex.getMessage());
                throw new Exception(ex.getMessage());
            }
            finally
            {
                session.close();
            }
        }

    }
}
